package top.api.service.impl;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import top.api.common.ResponseResult;
import top.api.enums.AppHttpCodeEnum;
import top.api.exception.SystemException;
import top.api.properties.OssProperties;
import top.api.service.OssUploadService;
import top.api.utils.OssUtils;
import top.api.utils.PathUtils;

import java.io.UnsupportedEncodingException;

@Service
@Slf4j
public class OssUploadServiceImpl implements OssUploadService {

    @Autowired
    private OssProperties ossProperties;

    @Override
    public ResponseResult uploadImg(MultipartFile file) {
        // 判断文件类型
        String originalFilename = file.getOriginalFilename();
        //对原始文件名进行判断
        if(StringUtils.hasText(originalFilename) && !originalFilename.endsWith(".png") && !originalFilename.endsWith("jpg")){
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }
        // 文件地址
        String filePath = PathUtils.generateFilePath(originalFilename);

        // 上传到七牛云oss
        String url = new OssUtils().ossUploadImg(ossProperties,file, filePath);

        return ResponseResult.okResult(url);
    }


}
